import request from '@/utils/request';

export interface UserInfo {
  id: number;
  username: string;
  nickname?: string;
  avatar?: string;
  phone: string;
  email?: string;
  bio?: string;
  gender?: string;
  isPhoneVerified: boolean;
  isEmailVerified: boolean;
  lastLoginAt?: string;
  createdAt: string;
  updatedAt: string;
}

export interface UpdateUserInfoParams {
  nickname?: string;
  bio?: string;
  email?: string;
  gender?: string;
  username?: string;
}

export const userApi = {
  // 获取用户信息
  getUserInfo: () => {
    console.log('调用 getUserInfo API');
    return request.get<UserInfo>('/user/info');
  },
  
  // 更新用户信息
  updateUserInfo: (data: UpdateUserInfoParams) => {
    console.log('调用 updateUserInfo API，数据:', data);
    return request.put<UserInfo>('/user/info', data);
  },
  
  // 上传头像
  uploadAvatar: (formData: FormData) => {
    return request.post<{url: string}>('/user/avatar', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    });
  },
  
  // 获取用户统计数据
  getUserStats: () => {
    return request.get<{
      favorites: number;
      following: number;
      followers: number;
      likes: number;
      participated: number;
      created: number;
      unreadMessages: number;
    }>('/user/stats');
  },
  
  // 修改密码
  changePassword(data: { oldPassword: string; newPassword: string }) {
    return request.post('/user/change-password', data);
  }
};
